TestCase("ListControllerInitTest", {
    setUp: function () {
        this.view = document.createElement("ol");
        this.backend = new tddjs.TodoList();
        this.controller = new tddjs.ListController(this.view, this.backend);

        this.addDone = function (text) {
            var backend = this.backend;

            this.backend.add(text, function (err, item) {
                backend.completeItem(item.id);
            });
        };

        this.addTodo = function (text) {
            this.backend.add(text);
        };
    },

    "test should display items from this.backend in list": function () {
        this.addTodo("Brush teeth");
        this.addTodo("Take shower");
        this.addTodo("Learn TDD");

        this.controller.init();

        var lis = this.view.childNodes;
        assertEquals(3, lis.length);
        assertEquals("Brush teeth", lis[0].innerHTML);
        assertEquals("Take shower", lis[1].innerHTML);
        assertEquals("Learn TDD", lis[2].innerHTML);
    },

    "test should display todo items with class todo": function () {
        this.addTodo("Brush teeth");

        this.controller.init();

        assertClassName("todo", this.view.firstChild);
    },

    "test should display done items with class done": function () {
        this.addDone("Brush teeth");

        this.controller.init();

        assertClassName("done", this.view.firstChild);
    },

    "test should sort todo items to the top": function () {
        this.addDone("Brush");
        this.addTodo("Shower");
        this.addDone("TDD");

        this.controller.init();

        var lis = this.view.childNodes;
        assertEquals("Shower", lis[0].innerHTML);
        assertEquals("Brush", lis[1].innerHTML);
        assertEquals("TDD", lis[2].innerHTML);
    },

    "test should display error message if server fails": function () {
        this.backend.all = function (callback) {
            callback({ message: "Server do bad" });
        };

        this.controller.init();

        assertClassName("error", this.view.firstChild);
        assertEquals("Server failed to get todo items",
                     this.view.firstChild.innerHTML);
    }
});

